<!-- ----------------------------
--  @author: 野狼上校           --
--  @create: 2024-07-17 10:06  --
--  @version: 1.0              --
-------------------------------->
<!-- 设备管理 -->

<template>
  <el-breadcrumb :separator-icon="DArrowRight" style='font: normal bold 1.1rem "Fira Sans", serif;'>
    <el-breadcrumb-item v-for="(item, index) in items" :key="index">{{ item }}</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider/>
  <!--  查询条件  -->
  <el-row :gutter="20" class="my-query">
    <el-col :span="6">
    </el-col>
    <el-col :span="8">
      <el-input placeholder="请输入公司名称" v-model="queryForm.companyName" clearable/>
    </el-col>
    <el-col :span="4">
      <el-button type="primary" :icon="Search" @click="initDevicesData">查询</el-button>
    </el-col>
    <el-col :span="6">
    </el-col>
  </el-row>

  <!-- 列表数据 -->
  <el-row :gutter="20" class="my-table">
    <el-col :span="4">
      设备列表
    </el-col>
    <el-col :span="16">
    </el-col>
    <el-col :span="4">
      <el-button type="success" :icon="Plus" @click="addDeviceHandler">新增设备</el-button>
    </el-col>
  </el-row>

  <!--  表格数据  -->
  <el-table :data="devices" stripe border style="width: 100%">
    <el-table-column prop="deviceGuid" label="设备Id" width="120" align="center"/>
    <el-table-column prop="deviceStatus" label="设备状态" width="120" align="center">
      <template #default="{row}">
        <!-- 当 deviceStatus 为 0 时 -->
        <el-tag v-if="row.deviceStatus === 0" size="large" type="danger">
          <span>离线</span>
        </el-tag>
        <!-- 当 deviceStatus 为 1 时 -->
        <el-tag v-else-if="row.deviceStatus === 1" size="small" type="success">
          <span>在线</span>
        </el-tag>
        <!-- 如果需要处理其他情况（可选） -->
        <el-tag v-else size="small" type="info">
          <span>未知状态</span>
        </el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="companyName" label="公司名称" width="100" align="center"/>
    <el-table-column prop="productionLine" label="生产线" width="120" align="center"/>
    <el-table-column prop="machineName" label="机床名称" width="120" align="center"/>
    <el-table-column prop="machineStatus" label="机床状态" width="120" align="center">
      <template #default="{row}">
        <!-- 当 deviceStatus 为 0 时 -->
        <el-tag v-if="row.machineStatus === 0" size="large" type="danger">
          <span>停止</span>
        </el-tag>
        <!-- 当 deviceStatus 为 1 时 -->
        <el-tag v-else-if="row.machineStatus === 1" size="small" type="success">
          <span>运行</span>
        </el-tag>
        <!-- 如果需要处理其他情况（可选） -->
        <el-tag v-else size="small" type="info">
          <span>未知状态</span>
        </el-tag>
      </template>
    </el-table-column>

    <el-table-column prop="updateTime" label="更新时间" width="120" align="center"/>
    <el-table-column prop="createTime" label="创建时间" width="120" align="center"/>


    <el-table-column prop="action" label="操作" min-width="300px" fixed="right" align="center">
      <template v-slot="scope">
        <!--     查看详情     -->
        <el-button type="primary" :icon="Search" @click="editDeviceHandler(scope.row.id)">查看</el-button>
        <!--     修改     -->
        <el-button type="primary" :icon="Edit" @click="editDeviceHandler(scope.row.id)">修改</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!--  分页插件  -->
  <el-pagination
      v-model:current-page="pagination.pageNum"
      v-model:page-size="pagination.pageSize"
      class="el-pagination"
      :page-sizes="[10, 20, 30, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
  />

  <!-- 新增 -->
  <AddDevice v-model="dialogAddDeviceVisible" :dialogAddDeviceVisible="dialogAddDeviceVisible"
             @initDevicesData="initDevicesData"/>
  <!-- 修改 -->
  <EditDevice v-model="dialogEditDeviceVisible" :dialogEditDeviceVisible="dialogEditDeviceVisible"
              :deviceRowInfo="deviceRowInfo" @initDevicesData="initDevicesData"/>
</template>

<script setup lang="ts">
import {useRoute} from "vue-router";
import {DArrowRight, Edit, Plus, Search} from "@element-plus/icons-vue";
import {onMounted, ref} from "vue";
import {Action, ElMessage, ElMessageBox} from "element-plus";
import {SUCCESS} from "@/utils/constants.ts";
import AddDevice from "@/views/crash/device/add.vue"
import EditDevice from "@/views/crash/device/edit.vue"
import {RespResult} from "@/api";
import {deleteDeviceById, getDevicesByPage, runDeviceByDeviceGuid, stopDeviceByDeviceGuid} from "@/api/crash/device.ts";

const breadcrumb: any = useRoute().meta.title;
const items: string[] = breadcrumb.split(",");

/*查询条件*/
const queryForm = ref({
  companyName: ""
});

/*表格数据*/
const devices = ref([]);

/*分页条件*/
const pagination = ref({
  pageNum: 1,
  pageSize: 10
})
/*总记录数*/
const total = ref(0);
/*改变每页显示数量*/
const handleSizeChange = (pageSize: number) => {
  pagination.value.pageNum = 1;
  pagination.value.pageSize = pageSize
  initDevicesData();
}
/*改变页码*/
const handleCurrentChange = (pageNum: number) => {
  pagination.value.pageNum = pageNum;
  initDevicesData();
}

/*初始化表格数据*/
onMounted(() => {
  initDevicesData();
})
const initDevicesData = async () => {
  //发送请求
  await getDevicesByPage({
    pageSize: pagination.value.pageSize,
    pageNum: pagination.value.pageNum,
    companyName: queryForm.value.companyName
  }).then(
      (response: RespResult) => {
        if (response.code === SUCCESS) {
          const jsonObject = JSON.parse(response.data);
          devices.value = jsonObject.records;
          total.value = jsonObject.total;
        } else {
          ElMessage({
            message: "获取数据失败",
            type: 'error'
          })
        }
      }
  )
}

/*新增*/
const dialogAddDeviceVisible = ref(false);
const addDeviceHandler = () => {
  //显示弹出框
  dialogAddDeviceVisible.value = true;
}

/*修改*/
const dialogEditDeviceVisible = ref(false);
const deviceRowInfo = ref({})
const editDeviceHandler = (row: any) => {
  //显示弹出框
  dialogEditDeviceVisible.value = true;
  deviceRowInfo.value = row;
}

/*运行*/
const runDeviceHandler = (row: any) => {
  runDeviceByDeviceGuid(row.deviceGuid).then(
      (response: RespResult) => {
        if (response.code === SUCCESS) {
          initDevicesData();
          ElMessage({
            message: "运行成功",
            type: "success"
          })
        } else {
          ElMessage({
            message: "运行失败",
            type: "error"
          })
        }
      }
  )
}

/*停止*/
const stopDeviceHandler = (row: any) => {
  stopDeviceByDeviceGuid(row.deviceGuid).then(
      (response: RespResult) => {
        if (response.code === SUCCESS) {
          initDevicesData();
          ElMessage({
            message: "停止成功",
            type: "success"
          })
        } else {
          ElMessage({
            message: "停止失败",
            type: "error"
          })
        }
      }
  )
}

/*删除*/
// const deleteDeviceHandler = (index: any, id: number) => {
//   ElMessageBox.confirm(
//       '你确定要删除吗?',
//       '警告:',
//       {
//         confirmButtonText: '确定',
//         cancelButtonText: '取消',
//         type: 'warning',
//       }
//   ).then(async () => {
//     await deleteDeviceById(id).then(
//         (response: RespResult) => {
//           if (response.code === SUCCESS) {
//             //从页面上删除记录
//             devices.value.splice(index, 1);
//             initDevicesData();
//             ElMessage({
//               message: "删除成功",
//               type: "success"
//             })
//           } else {
//             ElMessage({
//               message: "删除失败",
//               type: "error"
//             })
//           }
//         }
//     )
//   }).catch((action: Action) => {
//     console.log("取消或关闭" + action)
//   })
// }
</script>

<style scoped>
.my-query {
  margin-left: 0 !important;
  margin-right: 0 !important;
  margin-bottom: 20px;
  padding-top: 15px;
  padding-bottom: 15px;
  border: 1px solid #DDDDDD;
  box-sizing: border-box;
}

.my-table {
  margin-left: 0 !important;
  margin-right: 0 !important;
  margin-bottom: 5px;
  padding-top: 15px;
  padding-bottom: 15px;
  border: 1px solid #DDDDDD;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

</style>